/**
 * 工具方法
 */

/**
 * 计算金钱，默认保留2位小数
 * 超过一万，自动转换万元(财务不可用,仅展示商品使用)
 * @return { Object }
 */

export function formatMoney(money){
    if(!isNaN(money)){
        let moneyNumber = Number(money);
        let obj = {};
        if(moneyNumber > 10000){
            obj.unit = '万元',
            obj.num = (moneyNumber / 10000).toFixed(2);
            obj.dotBefore = obj.num.split('.')[0];
            obj.dotAfter = obj.num.split('.')[1];
            obj.text = obj.num + obj.unit;
        }else{
            obj.unit = '元',
            obj.num = (moneyNumber).toFixed(2);
            obj.dotBefore = obj.num.split('.')[0];
            obj.dotAfter = obj.num.split('.')[1];
            obj.text = obj.num + obj.unit;
        }
        return obj;
    }else{
        return {
            text:'0.00元',
            dotBefore:'0',
            dotAfter:'00',
            unit:'元',
            num:'0.00'
        }
    }
}

export function formatPrice(price){
    if(price){
        var temp = Number(price).toFixed(2);
        let obj = {
            dotBefore:temp.split('.')[0],
            dotAfter:temp.split('.')[1],
            price:temp
        }
        return obj;
    }else{
        return {
            dotBefore:'0',
            dotAfter:'00',
            price:'0.00'
        }
    }
}
export function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + ''
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
      }
    }
    return fmt
  }
   
  function padLeftZero(str) {
    return ('00' + str).substr(str.length)
  }